package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.RegimenNegocioDAO;
import com.bit.cfdi.web.domain.Negocio;
import com.bit.cfdi.web.domain.RegimenNegocio;
@Repository
public class RegimenNegocioDAOImpl extends HibernateDaoSupport implements RegimenNegocioDAO {

	@Autowired 
	public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@Override 
	@Transactional public void save(
			final RegimenNegocio regimenNegocio) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(regimenNegocio);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public RegimenNegocio getByDescription(
			final String nombreRegimenNegocio,
			final Negocio negocio) throws DataAccessException {
			List<RegimenNegocio> regimen = getHibernateTemplate().find(
					"from RegimenNegocio as r where r.regimen.descripcion=? " +
					"and r.negocio.idNegocio = ?", nombreRegimenNegocio, 
					negocio.getIdNegocio());    
			return !regimen.isEmpty()  ?  regimen.get(0) : null;
	}

	@Override
	@Transactional public void removeRegimenesNegocio(
			final List<RegimenNegocio> regimenesNegocio) throws DataAccessException {
		getHibernateTemplate().deleteAll(regimenesNegocio);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public RegimenNegocio getById(Long id) throws DataAccessException {
		List<RegimenNegocio> regimen = getHibernateTemplate().find(
				"from RegimenNegocio as r where r.idRegimenNegocio=? ",
				id);    
		return !regimen.isEmpty()  ?  regimen.get(0) : null;
	}

}
